package com.bw.usercenter.ui.mainview

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.JavascriptInterface
import android.webkit.JsResult
import android.webkit.WebChromeClient
import android.webkit.WebView
import com.bw.mvpcore.common.ConstValue
import com.bw.mvpcore.view.BaseMVPActivity
import com.bw.usercenter.R
import com.bw.usercenter.contract.UserCenterContract
import com.bw.usercenter.model.protocol.resp.RespRecommendEntity
import com.bw.usercenter.model.protocol.resp.RespSearchEntity
import com.bw.usercenter.model.protocol.resp.RespSearchitemIDEntity
import com.bw.usercenter.presenter.UserCenterPresenterImpl
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_details_view.*

class DetailsViewActivity : BaseMVPActivity<UserCenterPresenterImpl>(), UserCenterContract.UserCenterView {
    var data:List<RespSearchitemIDEntity.Data>?=null

    override fun getLayoutId(): Int {
        return R.layout.activity_details_view
    }
    @SuppressLint("JavascriptInterface")
    override fun initEvent() {
        var intent = intent
        var bundleExtra = intent.getBundleExtra(ConstValue.JUMP_ACTIVITY_PARSENTER_KEY)
        var itemid = bundleExtra?.getString("itemid")
        var itemUrl = bundleExtra?.getString("itemUrl")
        println("---$itemid--$itemUrl")

        var settings = my_webView.settings
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.javaScriptEnabled = true
        //my_webView.loadUrl("https://h5.m.taobao.com/awp/core/detail.htm?id=576761688896")
        my_webView.webChromeClient = object :WebChromeClient(){
            override fun onJsAlert(
                view: WebView?,
                url: String?,
                message: String?,
                result: JsResult?
            ): Boolean {
                return super.onJsAlert(view, url, message, result)
            }
        }

        my_webView.loadUrl("file:///android_asset/goods_msg.html")
        my_webView.addJavascriptInterface(getjava(),"javascript")
        mPresenter.getSearchitem(itemid.toString())

    }

    inner class getjava(){
        @JavascriptInterface
        fun call(s:String){

        }
    }


    override fun initData(savedInstanceState: Bundle?) {

    }

    override fun ClearStatBar() {

    }

    override fun releaseResource() {
    }

    override fun createPresenter(): UserCenterPresenterImpl {
        return UserCenterPresenterImpl(this@DetailsViewActivity)
    }

    override fun <T> registerSuccess(data: T) {
    }

    override fun registerSuccess(data: RespRecommendEntity) {

    }

    override fun registerFaild(throwable: Throwable) {

    }

    override fun registerSrech(respSearchEntity: RespSearchEntity) {

    }

    override fun getSearchitem(respSearchitemIDEntity: RespSearchitemIDEntity) {
        data = respSearchitemIDEntity.data
        var gson: Gson = Gson()

        var toJson = gson.toJson(respSearchitemIDEntity)
        println("-----$toJson")
        my_webView.loadUrl("javascript:setData("+"'"+toJson+"'"+")")
    }

    var string:String = "{\"code\":200,\"data\":[{\"CatLeafName\":\"帽子\",\"CatName\":\"服饰配件/皮带/帽子/围巾\",\"FreeShipment\":true,\"HGoodRate\":false,\"HPayRate30\":false,\"IRfdRate\":false,\"IsPrepay\":false,\"ItemUrl\":\"https://h5.m.taobao.com/awp/core/detail.htm?id=635225434169\",\"JuOnlineEndTime\":\"0\",\"JuOnlineStartTime\":\"0\",\"JuPlayEndTime\":0,\"JuPlayStartTime\":0,\"JuPreShowEndTime\":\"0\",\"JuPreShowStartTime\":\"0\",\"MaterialLibType\":\"1\",\"Nick\":\"tb9186079543的小店\",\"NumIid\":635225434169,\"PictUrl\":\"https://img.alicdn.com/bao/uploaded/i3/2210204300214/O1CN01jhBRi71DS3PIz8WfT_!!2210204300214-0-tblite.jpg\",\"PlayInfo\":null,\"PresaleDeposit\":\"0\",\"PresaleDiscountFeeText\":null,\"PresaleEndTime\":0,\"PresaleStartTime\":0,\"PresaleTailEndTime\":0,\"PresaleTailStartTime\":0,\"Provcity\":\"江苏 苏州\",\"Ratesum\":0,\"ReservePrice\":\"78.98\",\"SellerId\":2210204300214,\"ShopDsr\":0,\"SmallImages\":[\"https://img.alicdn.com/i2/2210204300214/O1CN01DPAUCX1DS3PF0OK4I_!!2210204300214-0-tblite.jpg\",\"https://img.alicdn.com/i2/2210204300214/O1CN01TZ4YQf1DS3PHhx37S_!!2210204300214-0-tblite.jpg\",\"https://img.alicdn.com/i3/2210204300214/O1CN01ZL1RNg1DS3PCc9HFQ_!!2210204300214-0-tblite.jpg\",\"https://img.alicdn.com/i3/2210204300214/O1CN01fSvrBF1DS3PAq4BgD_!!2210204300214-0-tblite.jpg\"],\"Title\":\"kk树男女全网眼儿童大阳鱼夫夏帽遮光防晒薄款中大童摭阳渔夫潮帽\",\"TmallPlayActivityEndTime\":0,\"TmallPlayActivityStartTime\":0,\"UserType\":0,\"Volume\":0,\"ZkFinalPrice\":\"78.98\"}],\"msg\":\"操作成功\"}"
}